package com.example.martian.service;

import com.alibaba.fastjson.JSONArray;
import com.example.martian.config.util.RedisUtils;
import com.example.martian.mapper.AuthenticationMapper;
import com.example.martian.mapper.RegisterMapper;
import com.example.martian.utils.*;
import com.mysql.cj.util.StringUtils;
import net.sf.json.JSONObject;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.DefaultApplicationArguments;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;


@Service
@Transactional
public class AutnenticationService {

    @Autowired
    private BASE64 base64;

    @Autowired
    private AuthenticationMapper authenticationMapper;

    @Autowired
    private TaskRunner runner;

    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Transactional(rollbackFor = Exception.class)
    public Object picUpOCR(MultipartFile picOcr, Integer userId, String username, String idNumber, MultipartFile picRear) throws IOException {
//        File file = new File(String.valueOf(picOcr));
//        //将File格式转换为MultipartFile格式
//        FileInputStream fileInputStream = new FileInputStream(file);
//        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
//                ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        JSONArray jsonArray = new JSONArray();
        Map<String,Object> authentiocationMap = new HashMap<>();
        try {
            Map<String, Object> map = base64.picOCR(picOcr);
            String name = map.get("姓名").toString();
            String number = map.get("公民身份号码").toString();
            if (name.equals(username) && number.equals(idNumber)){
                authentiocationMap.put("username",name);
                authentiocationMap.put("authenticationNumber",number);
                authentiocationMap.put("zt",1);
                authentiocationMap.put("userId",userId);
                authenticationMapper.updateAuthenticationZt(authentiocationMap);
                Timer timer = new Timer();
                TimerTask task = new TimerTask() {
                    public void run() {
                        authentiocationMap.put("username",name);
                        authentiocationMap.put("authenticationNumber",number);
                        authentiocationMap.put("zt",2);
                        authentiocationMap.put("userId",userId);
                        authenticationMapper.updateAuthenticationZt(authentiocationMap);
                    }
                };
                timer.schedule(task, 5*60*1000);
                JSONObject jsonObject = new JSONObject();
                Integer flg = registerMapper.selectAuthenticationZt(userId);
                jsonObject.put("zt",flg);
                jsonArray.add(jsonObject);
                return Utils.toJSON("提交成功",jsonArray.toString(),"0");
            }else {
                return Utils.toJSON("提交失败，请核实姓名和身份",Utils.toJSONmap(),"3");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Utils.toJSON("身份证识别错误，请重新拍摄",Utils.toJSONmap(),"1");
        }
    }

    //查询身份认证状态
    @Transactional
    public Object selectAuthenticationZt(Integer userId){
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        Integer flg = registerMapper.selectAuthenticationZt(userId);
        jsonObject.put("flg",flg);
        jsonArray.add(jsonObject);
        return Utils.toJSON("认证成功",jsonArray.toString(),"0");
    }

}
